package com.itheima.controller;


import com.itheima.service.FileService;
import com.itheima.pojo.File;
import com.itheima.pojo.FileSort;
import com.itheima.pojo.UpPictureVO;
import com.itheima.service.FileSortService;
import com.itheima.utils.IdWorker;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileService fileService;

    @Autowired
    private FileSortService fileSortService;

    /**
     * 获取图片信息接口
     * @param fileIds
     * @param code
     * @return
     */
    @GetMapping("/getPicture")
    public Map<String,Object> getPicture(@RequestParam("fileIds") String fileIds,@RequestParam("code") String code){
        Map<String,Object> result=new HashMap<>();
        //将查询数据封装成数组
        String[] ids = fileIds.split(code);
        Map[] data=new Map[ids.length];
        for (int i = 0; i < ids.length; i++) {
            Map map=new HashMap();
            File file = fileService.getPictureById(ids[i]);
            map.put("uid",file.getUid());
            map.put("fileOldName",file.getFileOldName());
            map.put("file_old_name",file.getFileOldName());
            map.put("name",file.getPicName());
            map.put("expandedName",file.getPicExpandedName());
            map.put("url",file.getPicUrl());
            data[i]=map;
        }
        //封装返回结果
        result.put("code","SUCCESS");
        result.put("data",data);
        return result;
    }

    /**
     * 多图片上传接口
     * @param upPictureVO
     * @param filedatas
     * @return
     * @throws Exception
     */
    @PostMapping("/pictures")
    public Map<String,Object> pictures(@RequestBody UpPictureVO upPictureVO, @RequestParam("filedatas") MultipartFile filedatas) throws Exception{
        String originalFilename = filedatas.getOriginalFilename();// 获取原始文件名
        int index = originalFilename.lastIndexOf(".");// 获取文件后缀坐标
        String extention = originalFilename.substring(index - 1);// 获取文件拓展名
        String fileName = new IdWorker(1,1).toString() + extention;//生成新的文件名
        //将文件上传到七牛云服务器
        QiniuUtils.upload2Qiniu(filedatas.getBytes(),fileName);
        //将图片信息存入数据库
        File file=new File();
        file.setUid(UUID.randomUUID().toString());
        file.setFileOldName(originalFilename);
        //根据sortName获取分类对象
        FileSort fileSort = fileSortService.getByFileSortName(upPictureVO.getSortName());
        //拼接图片存储路径
        file.setPicUrl("/"+fileSort.getProjectName()+"/"+fileSort.getSortName()+"/png/"+new SimpleDateFormat("yyyy/MM/dd").format(new Date())+fileName);
        file.setPicExpandedName(extention);
        file.setFileSize((int)filedatas.getSize());
        //根据sortName获取分类id
        String fileSortUid=fileSortService.getUidByFileSortName(upPictureVO.getSortName());

        file.setFileSortUid(fileSortUid);

        file.setAdminUid(upPictureVO.getSysUserId());
        file.setUserUid(upPictureVO.getUserUid());
        file.setStatus(1);

        file.setCreateTime(new Date());
        file.setUpdateTime(new Date());

        file.setQiNiuUrl("http://qemps0cxj.bkt.clouddn.com/"+fileName);
        fileService.add(file);

        //封装返回数据
        Map<String,Object> result=new HashMap<>();
        result.put("code","SUCCESS");
        result.put("message","上传成功");

        return result;
    }

}
